<template>
    <div class="address_manage">
        <header-bar title="地址管理" slideIn="1">
            <!-- <div slot="right" class="add_icon" @click="add">
                <img src="~@/assets/imgs/header/add_icon.png">
            </div> -->
        </header-bar>
        <div class="h10"> </div>
        <address-item :type="type" @itemClick="itemClick" @rightClick="rightClick" v-for="(item,index) in addressList" :key="index" :item="item"></address-item>
        <div class="no_data_block" v-if="addressList.length == 0 &&!isLoading">
          <no-data-panel :tipsData="tipsData"></no-data-panel>
        </div>
        <div class="btn marginBottom20"></div>
        <div class="btn_bg">
            <div class="btn_green_outline btn medium" @click="add">
                <img src="@/assets/imgs/account/add_adress_icon.png">
                <span>添加地址</span>
                
            </div>
        </div>

    </div>
</template>

<script>
import HeaderBar from 'components/header.vue'
import addressItem from './addressItem.vue'
import { mapState, mapActions } from 'vuex'
import native from '@/libs/native';
export default {
    components: {
        HeaderBar,
        addressItem,
    },
    created() {
        // this.type = this.$route.query.type||'2';
        this.list();
    },
    data(){
        return {
            addressList:[],
            type:"2",
            tipsData:{
                img:require('@/assets/imgs/common/no_adreess.png'),
                text:'还没添加收货地址哦～'
            },
            isLoading:true,
        }
    },
    methods: {
        ...mapActions(['showMsg', 'toggleLoading']),
        itemClick(item) {
            // sessionStorage.setItem('addressData',JSON.stringify(item));
            // this.$router.back();
            let type = this.$route.query.type;
            if(type == '1'){
                native.onSelectAddress(item);
            }
        },
        rightClick(item) {
            //选择地址
            if(this.type=='1'){
                native.onSelectAddress(item);
            }else{//编辑地址
                this.$router.push({ path: 'address', query: { id: item.id } });
            }
        },
        add() {
            this.$router.push('address');
        },
        list() {
            this.toggleLoading({flag:true});
            this.$ajax.getAddressList({}).then(res=>{
                this.toggleLoading({flag:false});
                this.isLoading = false;
                if(res.code=='0000'){
                    this.addressList = res.data;
                }
            }).catch(() => { this.toggleLoading({flag:false}); });;
        }
    }
}

</script>
<style lang="less" scoped>
.add_icon {
    width: .48rem;
}
.h10{
    height: .2rem;
}
.btn_bg{
    position: fixed;
    bottom: .4rem;
    left: 0;
    background: #fff;
    img{
        width: .32rem;
        height: .32rem;
    }
    span{
        margin-left: .1rem;
    }
}
.no_data_block{
    padding-top: 2rem;
}
.marginBottom20{
    margin-bottom: .4rem;
}
</style>

